package cc.mxtt.mvc.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import cc.mxtt.common.core.BaseController;
import cc.mxtt.common.core.GlobalKey;
import cc.mxtt.mvc.interceptor.UserLogInterceptor;
import cc.mxtt.mvc.model.Dept;
import cc.mxtt.mvc.model.Device;
import cc.mxtt.mvc.model.Project;
import cc.mxtt.mvc.model.Role;
import cc.mxtt.mvc.model.RolePower;
import cc.mxtt.mvc.model.User;
import cc.mxtt.mvc.model.UserRole;
import cc.mxtt.mvc.service.DevService;
import cc.mxtt.mvc.service.impl.DevServiceLmpl;

import com.jfinal.aop.Before;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;

public class DeviceController extends BaseController{
	
	DevService service = new DevServiceLmpl();
	public void index(){
		render("index.jsp");
	}
	
	
	public void list(){
		
		User user = getSessionAttr(GlobalKey.CurrentUser);
		Long deptId = user.getLong("dept_id");
		System.out.println("deptId:"+deptId);
		String sql = "select t.id,t.name,t.description,t.parent_id,t.order_id from sys_dept t where t.id=?";
		Dept dept = Dept.dao.findFirst(sql,deptId);
		Long id = dept.getLong("parent_id");
		System.out.println("parent_id:"+id);
		
		Long userId = user.getLong("id");
		
		UserRole menu = UserRole.dao.findFirst("select * from sys_user_role where user_id =?" , userId);
		Role role = Role.dao.findFirst("select * from sys_role where id =?", menu.getLong("role_id"));
		String roleName = role.getStr("name");
		
		int pageNumber = getParaToInt("page",1);
		int pageSize = getParaToInt("rows",10);
		String name = getPara(0,""); //搜索条件，如果没有则为空
		try {
			name = URLDecoder.decode(name, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Page<Device> listuser = null;
		if(roleName.equals("系统管理员")){
			listuser = service.getDataGrid(pageNumber, pageSize, name );
		}else{
			listuser = service.getDataGrid(pageNumber, pageSize, name , id);
		}
		
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", listuser.getTotalRow());
		result.put("rows", listuser.getList());
		System.out.println("devices:"+JsonKit.toJson(result).toString());
		renderJson(JsonKit.toJson(result));
		
		
	}
	
	@Before(UserLogInterceptor.class)
	public void add(){
		
		Project pro = new Project();
		//id=  name=ss  address=ss  jiandu=ss  jiance=ss  jianli person=sss  time=2016-03-11  jianshe=ss  sheji=ss  kancha=ss  jizhuang=ss  weituo=ss
		User user = getSessionAttr(GlobalKey.CurrentUser);
		Long deptId = user.getLong("dept_id");
		System.out.println("userId:"+deptId);
		String sql = "select t.id,t.name,t.description,t.parent_id,t.order_id from sys_dept t where t.id=?";
		Dept dept = Dept.dao.findFirst(sql,deptId);
		Long id = dept.getLong("parent_id");
		Map<String, Object> result = new HashMap<String, Object>();
		Long userId = user.getLong("id");
		boolean is  = RolePower.dao.getRole(userId , 5);
		if(!is){
			result.put("flag", false);
			result.put("Msg", "添加失败，请检查权限");
			renderJson(JsonKit.toJson(result));
			return;
		}
				
				
		
		System.out.println("dev add");
		
		Device dev =new Device();
		dev.set("name", getPara("name"));
		dev.set("type", getPara("type"));
		dev.set("dept_id", getPara("dept_id"));
		dev.set("dev_num", getPara("dev_num"));
		dev.set("zheng_num", getPara("zheng_num"));
		dev.set("time", getPara("time"));
		dev.set("jianding", getPara("jianding"));
		dev.set("parent_id", id);
		System.out.println(dev.toString());
		
		
		if (service.create(dev)){
			result.put("flag", true);
			result.put("Msg", "添加成功");
		}else{
			result.put("flag", false);
			result.put("Msg", "添加失败");
		}
		renderJson(JsonKit.toJson(result));
		
		
	}
	
	@Before(UserLogInterceptor.class)
	public void edit(){
		/**
		 * id=1  dept_id=  name=测试  type=测试类型   dev_num=0511  zheng_num=2011  time=2016-03-10  jianding=kipeng
		 */
		
		Map<String, Object> result = new HashMap<String, Object>();
		User user = getSessionAttr(GlobalKey.CurrentUser);
		Long userId = user.getLong("id");
		boolean is  = RolePower.dao.getRole(userId , 6);
		if(!is){
			result.put("flag", false);
			result.put("Msg", "修改失败,请检查权限");
			renderJson(JsonKit.toJson(result));
			return;
		}
		
		Device dev = service.findById(getPara("id"));
		System.out.println("id:"+dev.getLong("id"));
		dev.set("name", getPara("name"));
		dev.set("type", getPara("type"));
		dev.set("dept_id", getPara("dept_id"));
		dev.set("dev_num", getPara("dev_num"));
		dev.set("zheng_num", getPara("zheng_num"));
		dev.set("time", getPara("time"));
		dev.set("jianding", getPara("jianding"));
		System.out.println(dev.toString());
		
		
		if(service.update(dev)){
			result.put("flag", true);
			result.put("Msg", "修改成功");
		}else{
			result.put("flag", false);
			result.put("Msg", "修改失败");
		}
		renderJson(JsonKit.toJson(result));
		
	}
	
	
	@Before(UserLogInterceptor.class)
	public void delete(){
		System.out.println("delete：");
		Map<String, Object> result = new HashMap<String, Object>();
		User user = getSessionAttr(GlobalKey.CurrentUser);
		Long userId = user.getLong("id");
		boolean is  = RolePower.dao.getRole(userId , 7);
		if(!is){
			result.put("flag", false);
			result.put("Msg", "删除失败，请检查权限");
			renderJson(JsonKit.toJson(result));
			return;
		}
		boolean flag = true;
		String Msg = "";
		System.out.println("delete："+getPara("id"));
		if(service.delete(getPara("id"))){
			result.put("flag", true);
			flag = true;
			Msg = "删除成功";
			
		}else{
			flag = false;
			Msg = "删除失败";
		}
		
		result.put("flag", flag);
		result.put("Msg", Msg);
		renderJson(JsonKit.toJson(result));
		
	}
	
	@Before(UserLogInterceptor.class)
	public void lock(){
		Map<String, Object> result = new HashMap<String, Object>();
		boolean flag = true;
		String Msg = "";
		System.out.println("lock："+getPara("id"));
		if(service.lock(getPara("id"))){
			result.put("flag", true);
			flag = true;
			Msg = "锁定成功";
			
		}else{
			flag = false;
			Msg = "锁定失败";
		}
		
		result.put("flag", flag);
		result.put("Msg", Msg);
		renderJson(JsonKit.toJson(result));
		
		
	}
	
	@Before(UserLogInterceptor.class)
	public void unLock(){
		
		Map<String, Object> result = new HashMap<String, Object>();
		boolean flag = true;
		String Msg = "";
		System.out.println("lock："+getPara("id"));
		if(service.unLock(getPara("id"))){
			result.put("flag", true);
			flag = true;
			Msg = "解锁成功";
			
		}else{
			flag = false;
			Msg = "解锁失败";
		}
		
		result.put("flag", flag);
		result.put("Msg", Msg);
		renderJson(JsonKit.toJson(result));
		
	}
	
	
	

}
